###########################################
#  Primary Divisions: How Voters Evaluate Policy and Group Differences in Intra-Party Contests
#   - Forthcoming at The Journal of Politics
#   - Henderson et al 2021
#
###########################################
#  - code by S. Goggin & J. Henderson
########################################################
# This file produces densities/descriptives for candidates' ideology scores
########################################################

#dirs="~/Dropbox/replication0/"
#dirs should be set here or in runR.R 

rm(list=ls()[which(ls()!='dirs')])

library(ggplot2)
library(stringr)

# messy function to reorder by some estimate value
lableOrder=function(xmat,labels,label.groups,omits,o.column){

	# denote which label is to be omitted on the label
	for(i in 1:length(omits)){
		labels[which(labels==omits[i])]=paste('omit',labels[which(labels==omits[i])],sep='_')
	}

	# break groups into levels
	un_group=unique(label.groups)

	# this is the item to sort on, typically global or independent
	xm=xmat[,o.column]

	# vector which will contain row order
	xo=1:length(xm)

	# rearranging roworder within level
	for(j in 1:length(un_group)){
		ix=which(label.groups==un_group[j])
		if(length(ix)>2){
			ix=ix[!grepl(labels[ix],pattern='omit')]
			xo[ix]=xo[ix][order(xm[ix])]
		}
	}
	return(xmat[xo,])
}

reOrder=function(x,o){
	ix=array(NA,nrow(x))
	for(i in 1:length(o)){
		ix[i]=which(x$iv_order==o[i])
	}
	return(x[ix,])
}

setwd(dirs)
load("data/cces_stacked_unmatched.Rdata")

###########################################
###First, need to stack based on candidates, not just candidate pairs (and also get text out for labels later)

#This has leaners as independents, which is incorrect
#cces_stacked$pid3clean <- ifelse(cces_stacked$pid3=="Democrat",-1,ifelse(cces_stacked$pid3=="Republican",1,0))

library(car)

load("data/data_matrix_scored.Rdata")
candidate_matrix=data_matrix
#read.csv("data/data_matrix_scored.c",header=T,stringsAsFactors=F)[,-c(1)]

#####################################################################
# SUBSET ON VOTE CONDITIONS
vote <- subset(candidate_matrix,candidate_matrix$conjoints==3|candidate_matrix$conjoints==4|candidate_matrix$conjoints==7|candidate_matrix$conjoints==8)


{
# color party by neg tone
pdf('appendix/figures/rating_density_conjoints_3_4_7_8.pdf')
ymax=1.
plot(density(x=c(-1000,-1001)),axes=F,xlim=c(-2.5,2.5),ylim=c(0,ymax),lty=3,col='white',xlab='Conjoint Candidate Ideology Score',main='')
axis(1)
axis(2)

x2=density(na.rm=T,vote$scores[which(vote$pty==0)])#,bw=.075)
polygon(x=x2$x,y=x2$y,col="#00008B99",border=NA)
abline(v=mean(vote$scores[which(vote$pty==0)]),lty=2,col="darkblue")
text(cex=1.5,labels=expression(mu == -0.741),x=-1.48,y=(2.25/3)*ymax)

x1=density(na.rm=T,vote$scores[which(vote$pty==1)])#,bw=.075)
polygon(x=x1$x,y=x1$y,col="#FF000099",lty=2,border=NA)
abline(v=mean(vote$scores[which(vote$pty==1)]),lty=2,col="red")
text(cex=1.5,labels=expression(mu == 0.899),x=1.48,y=(2.4/3)*ymax)

legend(cex=1.5,'topleft',border=F,box.lty=0,legend=c('Republican','Democrat'),
	col=c('#FF000099','#00008B99'),fill=c('#FF000099','#00008B99'))
#t.test(n_ads[which(n_pty==-1)],n_ads[which(n_pty==1)])$p.value
#text(cex=1.5,labels=expression(p == '0.000'),x=.8,y=(2.95/3)*ymax)
dev.off()
}


#######################################################
# example figure on representative candidates

dems=subset(candidate_matrix,candidate_matrix$conjoints==3|candidate_matrix$conjoints==7)
reps=subset(candidate_matrix,candidate_matrix$conjoints==4|candidate_matrix$conjoints==8)

dQ=quantile(dems$scores,prob=c(.0244,.5228,.975))
rQ=quantile(reps$scores,prob=c(.0245,.5,.975))

dL=which(abs(dems$scores-dQ[1])==min(abs(dems$scores-dQ[1])))
dC=which(abs(dems$scores-dQ[2])==min(abs(dems$scores-dQ[2])))
dR=which(abs(dems$scores-dQ[3])==min(abs(dems$scores-dQ[3])))

rL=which(abs(reps$scores-rQ[1])==min(abs(reps$scores-rQ[1])))
rC=which(abs(reps$scores-rQ[2])==min(abs(reps$scores-rQ[2])))
rR=which(abs(reps$scores-rQ[3])==min(abs(reps$scores-rQ[3])))

# example plotting starts here
{
coefs=c(dems$scores[c(dL[1],dC[1])],reps$scores[c(rL[1])],dems$scores[c(dR[1])],reps$scores[c(rC[1],rR[1])] )
twords=cbind(
	t(rbind(dems[dL[1],8:16],dems[dC[1],8:16])),
	t(rbind(reps[rL[1],8:16])),
	t(rbind(dems[dR[1],8:16])),
	t(rbind(reps[rC[1],8:16],reps[rR[1],8:16]))
	)
coefs=2*(coefs/(max(coefs,na.rm=T)+.1))

coefs[5]=coefs[5]-.02

twords[6,]=c(
	"Civil rights groups",
	"Labor unions",
	"Business groups",
	"Business groups",
	"Christian groups",
	"Gun rights groups"
)

twords[7,]=c(
	"Powerful legislative committee",
	"Work across the aisle",
	"Work across the aisle",
	"Powerful legislative committee",
	"Refuse to compromise principles",
	"Stand with my party"
)


twords[8,]=c(
	"Combat global warming",
	"Strengthen gun control",
	"Oppose unfair foreign trade",
	"Prosecute government assistance abuse",
	"Raise taxes on the wealthy",
	"Strengthen border security"
)


twords[9,]=c(
	"Defend LGBT rights",
	"Promote free trade",
	"Immigrants path to citizenship",
	"Oppose unfair foreign trade",
	"Protect gun owners' rights",
	"Defend traditional marriage"
)

attributes=c('Gender','Race','Religion','Occupation','Personality','Endorsements','Record','Issue 1','Issue 2')

pdf(height=5,width=12,'appendix/figures/example_candidates.pdf')
#	plot(y=c(-1000),x=c(-1000),xlim=c(-1.84,2.21),col='white',ylim=c(-1.75,1.75),
	plot(y=c(-1000),x=c(-1000),xlim=c(-2.39,2.3),col='white',ylim=c(-1.75,1.75),
		axes=F,xlab='',ylab='',cex=.9)
	lines(y=c(0,0),x=c(-2,2))
	qq=seq(-2,2,by=1)
  	for(i in c(qq)){
		lines(y=c(-.035,.035),x=c(i,i))
		text(col='black',cex=.5,y=c(-.12),x=i,labels=c(as.character(i)))
  	}

  	cexs=.8
	#hmcols<-colorRampPalette(c("blue","purple3","red"))(length(coefs))
	hmcols=c(rep('blue',2),rep('red',1),rep('blue',1),rep('red',2))
	odd=c(1,1,-1,1,-1,-1)

	twords[,which(odd==-1)]=twords[nrow(twords):1,which(odd==-1)]

  	for(j in 1:length(coefs)){
		add=-.25
		#if(!is.even(j)){

			#add=-.1
		#} else{
		#	odd=1
			#add=.1
		#}
		#if(j==5){add=-.1}
		#if(j==7){add=-.1}
		#if(j==9){add=.1}
		#if(j==15){add=-.1}
		points(y=c(0),x=coefs[j],pch=16,cex=.65,col=hmcols[j])
		text(pos=4,x=coefs[j]+add,labels=twords[1,j],y=odd[j]*1.80,cex=cexs,col=hmcols[j])
		text(pos=4,x=coefs[j]+add,labels=twords[2,j],y=odd[j]*1.60,cex=cexs,col=hmcols[j])
		text(pos=4,x=coefs[j]+add,labels=twords[3,j],y=odd[j]*1.40,cex=cexs,col=hmcols[j])
		text(pos=4,x=coefs[j]+add,labels=twords[4,j],y=odd[j]*1.20,cex=cexs,col=hmcols[j])
		text(pos=4,x=coefs[j]+add,labels=twords[5,j],y=odd[j]*1.00,cex=cexs,col=hmcols[j])
		text(pos=4,x=coefs[j]+add,labels=twords[6,j],y=odd[j]*0.80,cex=cexs,col=hmcols[j])
		text(pos=4,x=coefs[j]+add,labels=twords[7,j],y=odd[j]*0.60,cex=cexs,col=hmcols[j])
		text(pos=4,x=coefs[j]+add,labels=twords[8,j],y=odd[j]*0.40,cex=cexs,col=hmcols[j])
		text(pos=4,x=coefs[j]+add,labels=twords[9,j],y=odd[j]*0.20,cex=cexs,col=hmcols[j])

	}

	text(pos=4,x=-2.55,labels=c(attributes),y=-1*seq(from=.2,to=1.8,by=.2),cex=cexs*.9,col='grey60')
	text(pos=4,x=-2.55,labels=c(attributes),y=1*seq(from=1.8,to=.2,by=-.2),cex=cexs*.9,col='grey60')

	lines(y=c(2.,.05),x=c(-2.08,-2.08),col='grey40',lty=2)
	lines(y=c(-2.,-.05),x=c(-2.08,-2.08),col='grey40',lty=2)

	legend(cex=1.3,x=1.65,y=1.875,border=F,box.lty=0,legend=c('Republican','Democrat'),
#		col=c('#FF000099','#00008B99'),fill=c('#FF000099','#00008B99'))
		col=c('red','blue'),fill=c('red','blue'))

dev.off()
}

#END
